#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitializeUi();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitializeUi()
{
    // 初始化表头
    QTableWidgetItem *header_item;
    QStringList header_string_list {"id", "姓名", "岁数"};
    ui->person_table->setColumnCount(header_string_list.count());
    for (int i = 0; i < header_string_list.count(); ++i) {
        header_item = new QTableWidgetItem {header_string_list.at(i)};
        QFont font = ui->person_table->font();
        font.setBold(true);
        font.setPointSize(12);
        header_item->setFont(font);
        header_item->setForeground(Qt::red);
        ui->person_table->setHorizontalHeaderItem(i, header_item);
    }
}


void MainWindow::on_connect_btn_clicked()
{
    sqlite_helper.Initialize("sqlite_test", "sqlite_test");
}


void MainWindow::on_refresh_btn_clicked()
{
    RefreshTable(ui->person_table);
}

void MainWindow::SetItem2TableWidget(QTableWidget *const table, int row_number, const QString &text, const CellType &cell_type,
                                     const FieldColumnNumber &field_column_number)
{
    QTableWidgetItem *item = new QTableWidgetItem(text, static_cast<int>(cell_type));
    item->setTextAlignment(Qt::AlignCenter);
    table->setItem(row_number, static_cast<int>(field_column_number), item);
}

void MainWindow::SetItem2TableWidget(QTableWidget *const table, int row_number, const QString &text, const CellType &cell_type,
                                     const FieldColumnNumber &field_column_number, Qt::Alignment alignment)
{
    QTableWidgetItem *item = new QTableWidgetItem(text, static_cast<int>(cell_type));
    item->setTextAlignment(alignment);
    table->setItem(row_number, static_cast<int>(field_column_number), item);
}

void MainWindow::RefreshTable(QTableWidget * const table)
{
    auto query_opt = sqlite_helper.ExecuteQuery(std::unique_ptr<SqliteQueryInterface> { new ReadFromPersonByPageQuery(1, 5) });

    if (query_opt.has_value()) {
        auto query = query_opt.value();
        std::vector<Person> person_list;

        while (query.next()) {
            person_list.emplace_back<Person>({ query.value(0).toInt(), query.value(1).toString(), query.value(2).toInt() });
        }
        table->setRowCount(person_list.size());

        int row_index = 0;
        for (auto &person : person_list) {
            SetItem2TableWidget(ui->person_table, row_index, QString::number(person.GetId()), CellType::kId, FieldColumnNumber::kId);
            SetItem2TableWidget(ui->person_table, row_index, person.GetName(), CellType::kName, FieldColumnNumber::kName);
            SetItem2TableWidget(ui->person_table, row_index, QString::number(person.GetAge()), CellType::kAge, FieldColumnNumber::kAge);
            ++row_index;
        }
    }
}

void MainWindow::on_add_btn_clicked()
{
    QString name = ui->add_name_edit->text();
    uint32_t age = ui->add_age_edit->text().toInt();
    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface> { new InsertPersonCommand(name, age) });
    RefreshTable(ui->person_table);
}


void MainWindow::on_update_btn_clicked()
{
    QString name = ui->update_name_edit->text();
    uint32_t age = ui->update_age_edit->text().toInt();
    uint32_t id = ui->update_id_edit->text().toInt();
    sqlite_helper.ExecuteCommand(std::unique_ptr<SqliteCommandInterface> { new UpdatePersonCommand(id, name, age) });
    RefreshTable(ui->person_table);
}

